package com.java.comprehensive.arithmetic.leecode.tags.linkedlist;

import util.ListNode;

/**
 * @Author: Dgg
 * @Date: 2021/12/14 21:15
 */
public class LeetCode_206 {

	public static ListNode reverseList(ListNode head) {
		return is(head);
	}

	public static ListNode is(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode listNode = is(head.next);
		/**    利用指针将head 的下一个的下一个只想自己 其实就是将listnode 放在head 后面*/
		// 4< 5>4  3>4>3
		head.next.next = head;
		/** 因为将listnode 放到自己后面 就变成了环，这里断开*/
		//3 4>3
		//4 5>4>3>2>1
		head.next = null;
		return listNode;
	}

	public static ListNode reverseListTwo(ListNode head) {
		ListNode prev = null;
		ListNode curr = head;
		while (curr != null) {
			ListNode next = curr.next;
			curr.next = prev;
			prev = curr;
			curr = next;
		}
		return prev;
	}


	public static ListNode reverseListThree(ListNode head) {
		ListNode dummyHead = new ListNode(0);
		dummyHead.next = head;
		while (head != null && head.next != null) {
			ListNode dummyNext = dummyHead.next;
			ListNode temp = head.next;
			dummyHead.next = temp;
			head.next = temp.next;
			temp.next = dummyNext;
		}

		return dummyHead.next;
	}


	public static void main(String[] args) {
		ListNode listNode3 = new ListNode(5, null);
		ListNode listNode4 = new ListNode(4, listNode3);
		ListNode listNode5 = new ListNode(3, listNode4);
		ListNode listNode6 = new ListNode(2, listNode5);
		ListNode listNode7 = new ListNode(1, listNode6);
		ListNode listNode = reverseListTwo(listNode7);
		System.out.println(listNode);
	}
}
